package com.example.analysis.service.impl;

import com.example.analysis.domain.EvaluateListInfo;
import com.example.analysis.dto.TimeScoreSupDto;
import com.example.analysis.domain.ScoreInfo;
import com.example.analysis.domain.SupervisionListInfo;
import com.example.analysis.entity.Course;
import com.example.analysis.entity.EvaluateSupervision;
import com.example.analysis.entity.PageBean;
import com.example.analysis.mapper.CourseDao;
import com.example.analysis.mapper.EvaluateSupervisionDao;
import com.example.analysis.service.EvaluateSupervisionService;
import com.example.analysis.utils.Response;
import com.example.analysis.vo.TimeScoreVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EvaluateSupervisionServiceImpl implements EvaluateSupervisionService {
    @Resource
    private EvaluateSupervisionDao evaluateSupervisionDao;

    @Resource
    private CourseDao courseDao;

    @Override
    public Response<EvaluateSupervision> insert(EvaluateSupervision aEvaluateListStudentList) {
        try {
            evaluateSupervisionDao.insert(aEvaluateListStudentList);
            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<List<EvaluateSupervision>> getById(String supervisionId, String subId, String teacherId) {
        List<EvaluateSupervision> aEvaluateListStudentList = evaluateSupervisionDao.selectByPrimaryKey(supervisionId,subId,teacherId);
        if (aEvaluateListStudentList != null) {
            return new Response<>(true, "Success", aEvaluateListStudentList);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public Response<List<TimeScoreVo>> getTimeScoreSet(String teacherId, String subId) {
        List<TimeScoreSupDto> timeScoreSetSup = evaluateSupervisionDao.getTimeScoreSetSup(teacherId, subId);
        if(timeScoreSetSup.isEmpty()) {
            return new Response<>(false, "Administration not found", null);
        }


        // 遍历timeScoreSetSup 将分数赋值到timeScoreVo再将查找出来的评价时间赋值给timeScoreVo
        List<TimeScoreVo> timeScoreVo = new ArrayList<>();
        for(TimeScoreSupDto timeScoreSupDto : timeScoreSetSup) {
            TimeScoreVo timeScoreVoTemp = new TimeScoreVo();

            timeScoreVoTemp.setScore(timeScoreSupDto.getScore());

            try {
                // 定义输入和输出格式
                SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");

                // 解析字符串为 Date 对象
                Date date = inputFormat.parse(evaluateSupervisionDao.getEvaTime(timeScoreSupDto.getSupervisionId(), timeScoreSupDto.getSubId(), timeScoreSupDto.getTeacherId()));

                // 格式化为目标字符串
                String output = outputFormat.format(date);
                timeScoreVoTemp.setEvaTime(output);
            } catch (Exception e) {
                e.printStackTrace();
            }

            timeScoreVo.add(timeScoreVoTemp);
        }

        return new Response<>(true, "Success", timeScoreVo);
    }

    @Override
    public List<SupervisionListInfo> getTimeInfo(SupervisionListInfo supervisionListInfo) {
        List<SupervisionListInfo> list=evaluateSupervisionDao.getList(supervisionListInfo);
        List<SupervisionListInfo> list1=new ArrayList<>();
        int target=0;
        SupervisionListInfo supervisionListInfo1=new SupervisionListInfo();
        Double count=0.0;
        //求总体分数
        for (SupervisionListInfo e: list) {
            if(target==5){
                supervisionListInfo1.setSubId(e.getSubId());
                supervisionListInfo1.setEvaTime(e.getEvaTime());
                supervisionListInfo1.setSemester(e.getSemester());
                count+=e.getSupervisionScore();
                supervisionListInfo1.setSupervisionScore((double) Math.round(count));
                list1.add(supervisionListInfo1);
                count=0.0;
                target=0;
                supervisionListInfo1=new SupervisionListInfo();
                continue;
            }
            count+=e.getSupervisionScore();
            target++;
        }
        //排序
        list1.sort((t1, t2) ->t1.getEvaTime().compareTo(t2.getEvaTime()));
        return list1;
    }

    @Override
    public List<ScoreInfo> getCourse(String semester) {
        List<ScoreInfo> list=evaluateSupervisionDao.getCourse(semester);
        return list;
    }

    @Override
    public List<SupervisionListInfo> getDiffSemester(SupervisionListInfo supervisionListInfo) {
        List<SupervisionListInfo> list=evaluateSupervisionDao.getList(supervisionListInfo);
        List<SupervisionListInfo> list1=new ArrayList<>();
        int target=0;
        SupervisionListInfo supervisionListInfo1=new SupervisionListInfo();
        Double count=0.0;
        //求总体分数
        for (SupervisionListInfo e: list) {
            if(target==5){
                supervisionListInfo1.setSubId(e.getSubId());
                supervisionListInfo1.setEvaTime(e.getEvaTime());
                supervisionListInfo1.setSemester(e.getSemester());
                count+=e.getSupervisionScore();
                supervisionListInfo1.setSupervisionScore((double) Math.round(count));
                list1.add(supervisionListInfo1);
                count=0.0;
                target=0;
                supervisionListInfo1=new SupervisionListInfo();
                continue;
            }
            count+=e.getSupervisionScore();
            target++;
        }
        //排序
        list1.sort((t1, t2) ->t1.getEvaTime().compareTo(t2.getEvaTime()));
        List<SupervisionListInfo> list2=new ArrayList<>();

        // 根据semester分组并计算平均分
        Map<String, Double> averageScores = list1.stream()
                .collect(Collectors.groupingBy(
                        SupervisionListInfo::getSemester,
                        Collectors.averagingDouble(SupervisionListInfo::getSupervisionScore)
                ));
        averageScores.forEach((semester,avg)-> {
            SupervisionListInfo supervisionListInfo2=new SupervisionListInfo();
            supervisionListInfo2.setSemester(semester);
            supervisionListInfo2.setSupervisionScore(avg);
            list2.add(supervisionListInfo2);
        });
        return list2;
    }

    @Override
    public List<Course> getClassOne() {
        List<Course> list=evaluateSupervisionDao.getClassOne();
        return list;
    }

    @Override
    public PageBean getSupervision(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<SupervisionListInfo> studentList=evaluateSupervisionDao.getEvaluate();
        Page p=(Page) studentList;
        long total=p.getTotal();
        List result=p.getResult();
        PageBean pageBean=new PageBean();
        pageBean.setTotal(total);
        pageBean.setRows(result);//返回总条数和具体数据
        return pageBean;
    }

    @Override
    public PageBean getAllCourse(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Course> courseList=courseDao.getAll();
        Page p=(Page) courseList;
        long total=p.getTotal();
        List result=p.getResult();
        PageBean pageBean=new PageBean();
        pageBean.setTotal(total);
        pageBean.setRows(result);//返回总条数和具体数据
        return pageBean;

    }
}
